//
// Created by XMac on 16/12/13.
// Copyright (c) 2016 XBN. All rights reserved.
//

#import "LZSearchPresenter.h"
#import "NetworkServiceManager.h"
#import "NSString+Additions.h"
#import "CityManager.h"
#import "CitysModel.h"

extern NSString *customer;
extern NSString *bound;
static NSString *const kSellerHotSearch = @"/clientbasisInfoInstall/sellerHotsearch";
static NSString *const kAutoMatedKeywords = @"/clientbasisInfoInstall/automatedKeywords";
@implementation LZSearchPresenter


-(void)hotSearch:(NSString *)type onSuccess:(PresenterCommonSuccess)success onFail:(PresenterCommonFail)fail {
    NSString *urlStr = COMBINATION(customer,kSellerHotSearch);
    [[NetworkServiceManager manager] executGetTask:urlStr parameters:@{@"type": type} success:^(NSURLSessionDataTask *task, id resultData) {

        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:fail]) {
            SAFE_BLOCK_INVOKE(success, resultData);
        }
    }                                         fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(fail, error.localizedDescription);
    }];
}


-(void)keywordsSearch:(NSString *)keywords type:(int)type onSuccess:(PresenterCommonSuccess)success onFail:(PresenterCommonFail)fail{

    NSString *urlStr = COMBINATION(customer,kAutoMatedKeywords);
    NSDictionary *params = @{@"keywords":keywords,@"type":@(type),@"cityCode":[CityManager shareManager].currentShowCityModel.cityNO?:@""};
    
    [[NetworkServiceManager manager] executGetTask:urlStr parameters:params success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:fail]) {
            NSMutableArray *resultArray = @[].mutableCopy;
            
            NSArray *data = resultData[@"data"];
            data = [data sortedArrayUsingComparator:^NSComparisonResult(NSDictionary  *_Nonnull obj1, NSDictionary  *  _Nonnull obj2) {
                int o1type = [obj1[@"type"] intValue];
                int o2type = [obj2[@"type"] intValue];
                if (o1type > o2type) {
                    return NSOrderedDescending;
                } else if(o1type < o2type){
                    return NSOrderedAscending;
                }
                
                return NSOrderedSame;
            }];
            DLog(@"data %@",data);
            [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                NSMutableArray *rowArray = @[[NSString stringWithFormat:@"%@",obj[@"type"]]].mutableCopy;
                NSArray *keywords = obj[@"keywords"];
                [keywords enumerateObjectsUsingBlock:^(id  _Nonnull innerObj, NSUInteger innerIdx, BOOL * _Nonnull innerStop) {
                    NSString *name = innerObj[@"name"];
                    NSString *type = obj[@"type"];
                    NSString *innerId = innerObj[@"id"];
                    
                    if (!(!name || (NSNull *)name == [NSNull null]) || !(!type || (NSNull *)type == [NSNull null])) {
                        if (!(!innerId || (NSNull *)innerId == [NSNull null])) {
                            [rowArray addObject:COMBINATION(innerObj[@"name"],bound,[NSString stringWithFormat:@"%@",type],bound,innerObj[@"id"])];
                        }
                        else {
                            if (!(!name || (NSNull *)name == [NSNull null]) && !(!type || (NSNull *)type == [NSNull null])) {
                                [rowArray addObject:COMBINATION(name,bound,[NSString stringWithFormat:@"%@",type])];
                            }
                        }
                    }
                }];
                
                [resultArray addObject:rowArray];
            }];
            
            SAFE_BLOCK_INVOKE(success, @[resultArray]);
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(fail, error.localizedDescription);
    }];
}


@end
